<?php

namespace app\api\controller;

use app\admin\model\District;
use app\admin\model\product\Listo;
use app\admin\model\product\Lists;
use app\admin\model\product\Order;
use app\common\controller\Api;
use think\Request;

/**
 * 店铺接口
 */
class Stores extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new \app\admin\model\Stores();
    }

    /**
     * 申请店铺
     * @throws \think\Exception
     */
    public function create()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }

        $userId = $this->auth->id;
        $row = \app\admin\model\Stores::where('user_id', $userId)->find();
        if ($row) {
            if ($row->status == 0) {
                $this->error('店铺正在审核中！');
            }
            if ($row->status == 1) {
                $this->error('你已申请店铺！');
            }
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $data = $this->request->post();

        if (!$data['name'] || !$data['mobile'] || !$data['IDcard']) {
            $this->error('店铺名称，手机号码，店主身份证号码缺一不可！');
        }

        $sdata = [
            'mobile' => $data['mobile'],
            'name' => $data['name'],
            'IDcard' => $data['IDcard'],
            'status' => 0,
        ];

        if ($row) {
            $res = $row->save($sdata);
        } else {
            $sdata['user_id'] = $userId;
            $res = $this->model->create($sdata);
        }
        if (!$res) {
            $this->error('申请失败！');
        }
        $this->success('申请成功');
    }

    /**
     * 编辑店铺
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $data = $this->request->post();

        $id = $data['id'];
        $row = $this->model->find($id);
        if (!$row) {
            $this->error('该店铺不存在！');
        }

        unset($data['token']);
        $data['user_id'] = $this->auth->id;

        $count = $this->model->where(['user_id' => $data['user_id'], 'type' => $row->type, 'id' => ['<>', $id]])->count();
        if ($count == 0) {
            $data['status'] = 1;
            $this->model->allowField(true)->save($data, ['id' => $data['id']]);
        } else {
            if ($data['status'] == 1) {
                $this->model->where('user_id', $data['user_id'])->where('type', $row->type)->setField('status', 0);
            }
            $this->model->allowField(true)->save($data, ['id' => $data['id']]);
        }

        $this->success('编辑成功');
    }

    /**
     * 店铺详情
     */
    public function read()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $userId = $this->auth->id;
        $row = $this->model->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该店铺不存在!');
        }

        $row->createtime = date('Y-m-d H:i:s', $row->createtime);

        $id = $row->id;
        $total_numbers1 = 0;//销量
        $total_numbers2 = 0;//余量
        $total_money = 0;//销售额
        $lists = Lists::where('sid', $id)->select();
        if ($lists) {
            foreach ($lists as $val) {
                $total_numbers1 += $val->sell_num;
                $total_numbers2 += $val->goods_num;

                $money = Listo::where('product_id', $val->id)->where('status', '>=', 1)->sum('total');
                $total_money += $money;
            }
        }

        $row->total_numbers1 = $total_numbers1;
        $row->total_numbers2 = $total_numbers2;
        $row->total_money = $total_money;

        $this->success('店铺详情为', $row);
    }
}
